﻿/// <reference path="../../../../SIR.Web/scripts/_references.js" />
/// <reference path="../../../../SIR.Web/scripts/jquery-1.9.1.js" />
/// <reference path="../../../../SIR.Web/scripts/angular.js" />
/// <reference path="../../../../SIR.Web/scripts/angular-mocks.js" />

/// <reference path="../../../../SIR.Web/app/app.js" />
/// <reference path="../../../../SIR.Web/app/common/common.js" />

/// <reference path="../../../../SIR.Web.Tests/JavaScript/sir-mock.js" />

/// <reference path="../../../../SIR.Web/app/user/user.js" />
/// <reference path="../../../../SIR.Web/app/user/controllers/user-image-upload-controller.js" />

describe('User Controllers:', function () {
	'use strict';
	// declare variables and add modules
	var scope, location, controller, rootScope, mockLibraryMediator, notifyService;

	beforeEach(module('ui.router'));
	beforeEach(module('app.common'));
	beforeEach(module('app.user'));

	beforeEach(inject(function ($location, $controller, $rootScope, notify) {
		// initialize variables for the tests
		notifyService = notify;
		rootScope = $rootScope;
		scope = $rootScope.$new();
		location = $location;
		// intialize the model
		scope.model = {
			uploadFiles: [{}],
			message: ''
		};
		// set the get folder function to return 1
		scope.getFolderId = function () { return 1; };
		// mock mediator
		mockLibraryMediator = {
			setUploadController: function () { },
			upload: function () { },
		};
		// spy on the set upload controller method
		spyOn(mockLibraryMediator, 'setUploadController').andCallThrough();
		// initialize the controller
		controller = $controller('UserImageUploadController', {
			$scope: scope, 
			libraryMediator: mockLibraryMediator,
			$state: {}
		});
		// expect that the mediator was registered with the new controller
		expect(mockLibraryMediator.setUploadController).toHaveBeenCalled();
		// spy on mediator uplaod and the notify error/success methods
		spyOn(mockLibraryMediator, 'upload').andCallThrough();
		spyOn(notifyService, 'error').andCallThrough();
		spyOn(notifyService, 'success').andCallThrough();
	}));

	describe('Controller - User Upload Image (Total: 3)', function () {
		it('[1] Produce \'Invalid password\' message when user enters a valid email and an invalid password to upload an image', function () {
			// set upload files to a single file
			scope.model.uploadFiles = [{}];
			// attempt the upload
			scope.uploadFile();
			// expect the mediator was called
			expect(mockLibraryMediator.upload).toHaveBeenCalled();
			// fake response with unsuccessful response
			controller.handleImageUploaded([{ Successful: false, Message: 'Must be authenticated to upload an image to a folder' }]);
			// verify the error was shown to the user
			expect(notifyService.error).toHaveBeenCalledWith('Must be authenticated to upload an image to a folder');
		});

		it('[2] Produce \'Invalid image format\' message when user enters a invalid image format to upload an image', function () {
			// set upload files to a single file
			scope.model.uploadFiles = [{}];
			// attempt the upload
			scope.uploadFile();
			// expect the mediator was called
			expect(mockLibraryMediator.upload).toHaveBeenCalled();
			// fake response with unsuccessful response
			controller.handleImageUploaded([{ Successful: false, Message: 'Unsupported Media Type' }]);
			// verify the error was shown to the user
			expect(notifyService.error).toHaveBeenCalledWith('Unsupported Media Type');
		});

		it('[3] Produce \'Upload successful\' message when user enters a valid email, a valid password, and valid image format to upload an image', function () {
			// set upload files to a single file
			scope.model.uploadFiles = [{}];
			// attempt the upload
			scope.uploadFile();
			// expect the mediator was called
			expect(mockLibraryMediator.upload).toHaveBeenCalled();
			// send a successful response message to the user
			controller.handleImageUploaded([{ Successful: true, Message: '' }]);
			// verify the success message was shown to the user with the number of images uploaded
			expect(notifyService.success).toHaveBeenCalledWith('1 image uploaded.');
		});

	});
});